
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_errno.h"
#include "hi_io.h"
#include "hi_gpio.h"
#include "iot_gpio.h"
#include "iot_i2c.h"
#include "nfc.h"


#define I2C_TASK_STACK_SIZE 1024 * 8
#define I2C_TASK_PRIO 25
#define IIC_INDEX 1
#define IIC_BAUDRATE 400000
#define TEXT "Welcome to SPARK-HM-PI!"
#define WEB "www.nxrobo.com"

static void I2CTask(void)
{
    uint8_t ret;
    
    hi_gpio_init();
    // IoTGpioInit(0);
    // IoTGpioInit(1);
    //GPIO_0复用为I2C1_SDA
    hi_io_set_func(0, HI_IO_FUNC_GPIO_0_I2C1_SDA);
    //GPIO_1复用为I2C1_SCL
    hi_io_set_func(1, HI_IO_FUNC_GPIO_1_I2C1_SCL);
    //ret = IoTI2cInit(IIC_INDEX, IIC_BAUDRATE);
    ret = hi_i2c_init(IIC_INDEX, IIC_BAUDRATE);
    if(ret != IOT_SUCCESS)
    {
        printf("Init IIC failed!\n");
        return;
    }
    printf("Init IIC successful!\n");
    ret = storeText(NDEFFirstPos, (uint8_t *)TEXT);
    if (ret != 1)
    {
        printf("NFC Write Data Falied :%d ", ret);
    }
    ret = storeUrihttp(NDEFLastPos, (uint8_t *)WEB);
    if (ret != 1)
    {
        printf("NFC Write Data Falied :%d ", ret);
    }
    while (1)
    {
        printf("=======================================\r\n");
        printf("***********I2C_NFC_example**********\r\n");
        printf("=======================================\r\n");
        printf("Please use the mobile phone with NFC function close to the development board!\r\n");
        usleep(1000000);
    }
}

static void I2CExampleEntry(void)
{
    osThreadAttr_t attr;

    attr.name = "I2CTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = I2C_TASK_STACK_SIZE;
    attr.priority = I2C_TASK_PRIO;

    if (osThreadNew((osThreadFunc_t)I2CTask, NULL, &attr) == NULL)
    {
        printf("Falied to create I2CTask!\n");
    }
}

APP_FEATURE_INIT(I2CExampleEntry);